package com.boolib.fdfs.util;

import com.boolib.fdfs.ServerResponse;
import org.springframework.web.multipart.MultipartFile;

import java.io.FileInputStream;
import java.io.IOException;

public class ImageFileUtil {

    public  static boolean isImage(MultipartFile file) throws IOException {
        FileInputStream fileInputStream = null;

            fileInputStream = (FileInputStream) file.getInputStream();

        byte[] src = new byte[28];
        fileInputStream.read(src);
        StringBuffer stringBuffer  = new StringBuffer();



        for(int i =  0 ; i < src.length;i++){
            int v = src[i] & 0xFF;
            String hv = Integer.toHexString(v).toUpperCase();
            if (hv.length() < 2) {
                stringBuffer.append(0);
            }
            stringBuffer.append(hv);

        }

        String fileExt = stringBuffer.toString();


        FileType[] fileTypes = FileType.values();


        for (FileType fileType: fileTypes  ) {

            if(fileExt.startsWith(fileType.getValue())){
              return true;
            }else{

            }
        }
        return false;

    }
}
